package me.jzn.alib.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.VectorDrawable;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.WorkerThread;
import androidx.core.graphics.drawable.DrawableCompat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.OutputStream;

import me.jzn.alib.ALib;
import me.jzn.core.utils.CommUtil;

/**
 * @author xqs @date 2020/12/8
 */
public class BitmapUtil {
    private static final Logger log = LoggerFactory.getLogger(BitmapUtil.class);

    public static final Bitmap text2Bitmap(String text, int width, int height, int textSize, @ColorInt int bgColor, @ColorInt int fgColor) {
        Paint paint = new Paint();
        paint.setColor(fgColor);
        paint.setTextSize(textSize);
        //        paint.setTextSize(220);
        paint.setAntiAlias(true);
        //        int width = 480;
        //        int height = 480;
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(bgColor);
        //        canvas.drawColor(Color.parseColor(color));
        Rect rect = new Rect();
        paint.getTextBounds(text, 0, text.length(), rect);
        Paint.FontMetrics fm = paint.getFontMetrics();
        int textLeft = (int) ((width - paint.measureText(text)) / 2);
        int textTop = (int) (height - width / 2 + Math.abs(fm.ascent) / 2 - 25);
        canvas.drawText(text, textLeft, textTop, paint);
        return bitmap;
    }

    public static final Bitmap fileUri2Bitmap(Uri uri) {
        try {
            return MediaStore.Images.Media.getBitmap(CtxUtil.getContentResolver(), uri);
        } catch (IOException e) {
            log.warn("文件转bitmap失败", e);
            return null;
        }
    }

    public static final Drawable toDrawable(Bitmap bitmap) {
        if (bitmap == null) {
            return null;
        }
        return new BitmapDrawable(ALib.app().getResources(), bitmap);
    }

    public static final Bitmap fromDrawable(@DrawableRes int drawableRes) {
        Bitmap bitmap = BitmapFactory.decodeResource(ResUtil.getResources(), drawableRes);
        // svg为空
        if (bitmap == null) {
            Drawable drawable = ResUtil.getDrawable(drawableRes);
            return fromDrawable(drawable);
        }
        return bitmap;
    }

    public static final Bitmap fromDrawable(Drawable drawable) {
        if (drawable == null) {
            return null;
        }
        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable) drawable).getBitmap();
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            if (drawable instanceof VectorDrawable) {
                // 暂时啥也不用做
            }
        } else {
            drawable = (DrawableCompat.wrap(drawable)).mutate();
        }
        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), drawable
                .getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        // canvas.setBitmap(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        drawable.draw(canvas);
        return bitmap;
    }

    @WorkerThread
    public static final void save(Bitmap bitmap, Bitmap.CompressFormat fmt, int quality, OutputStream os) {
        try {
            bitmap.compress(fmt, quality, os);
        } finally {
            if (!bitmap.isRecycled()) {
                bitmap.recycle();
            }
            CommUtil.close(os);
        }
    }

}
